package com.zhengxiang.onlinexamination.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.zhengxiang.onlinexamination.entity.SysDept;
import com.zhengxiang.onlinexamination.entity.SysMenu;

import java.util.List;

/**
 * @author zhengxiang
 * @data 2023/3/11 10:39
 */
public class SafeTreeUtil {

    /**
     * 构建菜单树形结构
     *
     * @param list
     * @param pid  最顶层父级ID
     * @return
     */
    public static List<Tree<String>> createDeptTree(List<SysDept> list, String pid) {
        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("deptId");
        treeNodeConfig.setNameKey("deptName");
        treeNodeConfig.setParentIdKey("parentId");

        // 转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, pid, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getDeptId().toString());
            tree.setParentId(treeNode.getParentId().toString());
            tree.setName(treeNode.getDeptName());
            tree.putExtra("phone",treeNode.getPhone());
            tree.putExtra("leader",treeNode.getLeader());
            tree.putExtra("email",treeNode.getEmail());
            tree.putExtra("orderNum",treeNode.getOrderNum());
        });

        return treeNodes;
    }
    /**
     * 构建菜单树形结构
     *
     * @param list
     * @param pid  最顶层父级ID
     * @return
     */
    public static List<Tree<String>> createMenuTree(List<SysMenu> list, String pid) {
        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("menuId");
        treeNodeConfig.setNameKey("menuName");
        treeNodeConfig.setParentIdKey("parentId");

        // 转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, pid, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getMenuId().toString());
            tree.setParentId(treeNode.getParentId().toString());
            tree.setName(treeNode.getMenuName());
            tree.putExtra("menuType",treeNode.getMenuName());
            tree.putExtra("path",treeNode.getPath());
            tree.putExtra("visible",treeNode.getVisible());
            tree.putExtra("component",treeNode.getComponent());
            tree.putExtra("orderNum",treeNode.getOrderNum());
            tree.putExtra("icon",treeNode.getIcon());
            tree.putExtra("menuType",treeNode.getMenuType());
            tree.putExtra("perms",treeNode.getPerms());
        });



        return treeNodes;
    }
    public static void main(String[] args) {
        System.out.println(DateUtil.today());
    }
}
